﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace client
{
    public partial class Statki : Form
    {
        static string ip;
        static string ip1="127";
        static string ip2="0";
        static string ip3="0";
        static string ip4="1";
        
        static int port;
        static bool Ready;
        

        public Statki()
        {
            port = 5747;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            Ready = false ;

            if ((textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox3.Text.Length > 0 && textBox4.Text.Length > 0)
                && (port != 0))
            {
                Ready = true;
            }

            if (!Ready)
            {
                MessageBox.Show("Podaj IP i port Serwera!");
                return;
            }
            else
            {
                ip = (ip1 + "." + ip2 + "." + ip3 + "." + ip4).ToString();
                try
                {
                    Program.PolaczZSerwerem(ip, port);
                    Program.buforek[0] = 0;
                    Program.Serwer.Send(Program.buforek);
                    this.Enabled = false;
                    this.Visible = false;
                    Program.giera.Enabled = true;
                    Program.giera.Visible = true;
                } catch (Exception exp)
                {
                    if (Program.Serwer.Connected)
                        Program.Serwer.Disconnect(true);
                    MessageBox.Show("Błąd połączenia z serwerem!\nBład: "+ exp.ToString());
                }
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(textBox1.Text) > 255)
            {
                textBox1.Text = "";
                MessageBox.Show("Podales bledne IP");
                return;              
            }

            ip1 = textBox1.Text;

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            port = int.Parse(comboBox1.Text);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(textBox3.Text) > 255)
            {
                textBox3.Text = "";
                MessageBox.Show("Podales bledne IP");
                return; 
            }

            ip3 = textBox3.Text;

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(textBox2.Text) > 255)
            {
                textBox2.Text = "";
                MessageBox.Show("Podales bledne IP");
                return; 
            }

            ip2 = textBox2.Text;

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(textBox4.Text) > 255)
            {
                textBox4.Text = "";
                MessageBox.Show("Podales bledne IP");
                return; 
            }

            ip4 = textBox4.Text;

        }
    }
}
